﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Model;
using FastExecutor.Base.Util;
using FastExecutor.ORG.DB;
using FastExecutor.ORG.Entity;
using FastExecutor.ORG.Enum;
using FastIOC.Annotation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace FastExecutor.ORG.Controller
{
    [RouteArea("Frame")]
    [RoutePrefix("ModuleRight")]
    public class ModuleRightController : FastTemplateController<Frame_ModuleRight>
    {
        [AutoWired]
        public DB_Role @DB_Role;

        [Route("List")]
        public ActionResult List()
        {
            return View("~/Views/Frame/Module/ModuleRight_List.cshtml");
        }

        [Route("Edit")]
        public ActionResult Edit()
        {
            return View("~/Views/Frame/Module/ModuleRight_Edit.cshtml");
        }

        [HttpPost, Route("FindModuleRightDetail")]
        public ActionResult FindModuleRightDetail()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string ModuleGuid = Convert.ToString(Params.GetValue("RowGuid"));
            List<Frame_ModuleRight> AllRightList = DB_Fast.FindAll(string.Format(" and ModuleGuid='{0}'", ModuleGuid));
            List<Frame_ModuleRight> RoleRightList = AllRightList.Where(a => a.RightType == (int)FrameEnum.ModuleRightType.Role).ToList();
            List<Frame_ModuleRight> OURightList = AllRightList.Where(a => a.RightType == (int)FrameEnum.ModuleRightType.OU).ToList();
            List<Frame_ModuleRight> UserRightList = AllRightList.Where(a => a.RightType == (int)FrameEnum.ModuleRightType.User).ToList();
            List<Frame_Role> RoleList = @DB_Role.FindAll();
            List<Frame_ModuleRight> RoleRightFilerList = null;
            foreach (Frame_Role Role in RoleList)
            {
                RoleRightFilerList = RoleRightList.Where(a => a.RightGuid == Role.RowGuid).ToList();
                if (RoleRightFilerList.Count > 0)
                {
                    Role.IsCheck = true;
                }
                else
                {
                    Role.IsCheck = false;
                }
            }
            result.Add("RoleList", RoleList);
            List<string> OUGuidList = new List<string>();
            foreach (Frame_ModuleRight Right in OURightList)
            {
                OUGuidList.Add(Right.RightGuid);
            }
            result.Add("OUList", OUGuidList);
            List<string> UserGuidList = new List<string>();
            foreach (Frame_ModuleRight Right in UserRightList)
            {
                UserGuidList.Add(Right.RightGuid);
            }
            result.Add("UserList", UserGuidList);
            return result;
        }

        [HttpPost, Route("OperateModuleRightUpdate")]
        public ActionResult OperateModuleRightUpdate()
        {
            ReturnValue result = new ReturnValue();
            JObject Params = GetParams();
            string ExceptionStr = string.Empty;
            string ModuleGuid = Convert.ToString(Params.GetValue("RowGuid"));
            string RoleList = Convert.ToString(Params.GetValue("RoleList"));
            string OUList = Convert.ToString(Params.GetValue("OUList"));
            string UserList = Convert.ToString(Params.GetValue("UserList"));
            List<Frame_Role> RoleRightList = JsonConvert.DeserializeObject<List<Frame_Role>>(RoleList);
            List<string> OURightList = JsonConvert.DeserializeObject<List<string>>(OUList);
            List<string> UserRightList = JsonConvert.DeserializeObject<List<string>>(UserList);
            string ResertSQL = string.Format("delete from frame_moduleright where ModuleGuid='{0}'", ModuleGuid);
            @DBUtil.ExecuteSQL(ResertSQL);
            foreach (Frame_Role Role in RoleRightList)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = Role.RowGuid;
                Right.RightType = (int)FrameEnum.ModuleRightType.Role;
                DB_Fast.Insert(Right);
            }
            foreach (string OUGuid in OURightList)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = OUGuid;
                Right.RightType = (int)FrameEnum.ModuleRightType.OU;
                DB_Fast.Insert(Right);
            }
            foreach (string UserGuid in UserRightList)
            {
                Frame_ModuleRight Right = new Frame_ModuleRight();
                Right.ModuleGuid = ModuleGuid;
                Right.RightGuid = UserGuid;
                Right.RightType = (int)FrameEnum.ModuleRightType.User;
                DB_Fast.Insert(Right);
            }
            return result;
        }
    }
}
